package com.inossem.warehousestorage_inossem.activity.up_frame;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.extra.UpFrameExtra;
import com.inossem.warehousestorage_inossem.adapter.up_frame.UpFrameItemAdapter;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameOverviewBean;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.UpFrameUtils;

public class UpFrameItemActivity extends BaseActivity {

    private TextView title;

    private ListView list;

    private UpFrameItemAdapter adapter;

    private UpFrameOverviewBean.DataBean.MaterielListBean item;

    private String serialStr, mLgnum, mPosition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_up_frame_item);
        initView(UpFrameItemActivity.this);
        initData(UpFrameItemActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        list = (ListView) activity.findViewById(R.id.list);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_up_frame_item_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_up_frame_commit_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getExtra(activity);
        getData(activity);
        setmScanListener((String scanResult, int length) -> {
            mPosition = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity);
        }, QRCodeConstant.TYPE_POSITION);
    }

    private void getExtra(Activity activity) {
        serialStr = activity.getIntent().getStringExtra(UpFrameExtra.ITEM_SERIAL);
        mLgnum = activity.getIntent().getStringExtra(UpFrameExtra.LGNUM);

    }

    private void getData(Context activity) {
        item = UpFrameUtils.getMaterielBySerial(activity, serialStr);
        addUpFrameItemListData(activity, item, mLgnum, mPosition, list);
    }

    private void addUpFrameItemListData(Context context, UpFrameOverviewBean.DataBean.MaterielListBean bean, String lgnum, String position, ListView listView) {
        adapter = new UpFrameItemAdapter(context, bean, lgnum, position, new UpFrameItemListener() {
            @Override
            public void onNoDoubleClickListener(UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean positionItem) {
                UpFrameUtils.updatePositionListBySerial(context, serialStr, positionItem);
                mPosition = "";
                getData(context);
            }

            @Override
            public void onDeletePositionNumber(String id) {
                UpFrameUtils.deletePositionNumberByIdAndSerial(context, id, serialStr);
                mPosition = "";
                getData(context);
            }
        });
        listView.setAdapter(adapter);
    }

    public interface UpFrameItemListener {
        void onNoDoubleClickListener(UpFrameOverviewBean.DataBean.MaterielListBean.PositionListBean positionItem);

        void onDeletePositionNumber(String id);
    }
}
